package niuke;

import java.util.Stack;

public class Solution {

    public static void main(String[] args) {
        String str = "ccmm";
        String str1 = "cmmc";
        boolean b = magicString(str1);
        System.out.println(b);
    }

    public static boolean magicString(String s){
        char[] charArray = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        Stack<Character> tempStack = new Stack<>();
        for (char c : charArray) {
            stack.add(c);
        }
        while (!stack.isEmpty()){
            Character topChar = null;
            if(!tempStack.isEmpty()){
                topChar = tempStack.peek();
            }
            tempStack.add(stack.pop());
            if((tempStack.peek()+"").equals("c") && (topChar+"").equals("m")){
                tempStack.pop();
                tempStack.pop();
            }
        }
        return tempStack.isEmpty();
    }

    private static void print(Stack<Character> stack) {
        while (!stack.isEmpty()){
            System.out.println(stack.pop());
        }
    }
}
